package com.logos.io;

import java.io.*;

public class copyFile {


    public static void main(String[] args) {
        try {
            copyFileByByte("C:/学习/java/study-technology/study-javaSE/src/main/resources/source.txt",
                        "C:/学习/java/study-technology/study-javaSE/src/main/resources/destination.txt");
            System.out.println("文件拷贝成功！");
        } catch (IOException e) {
            System.out.println("文件拷贝失败：" + e.getMessage());
        }
    }

    // 文件拷贝字节流
    public static void copyFileByByte(String sourcePath, String destPath) throws IOException {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(sourcePath));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destPath));
        byte[] buffer = new byte[1024];
        int length;
        while ((length = bis.read(buffer)) != -1) {
            bos.write(buffer, 0, length);
        }
        bos.close();
        bis.close();
    }

    // 文件拷贝字符缓冲流
    public static void copyFileByChar(String sourcePath, String destPath) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(sourcePath));
        BufferedWriter bw = new BufferedWriter(new FileWriter(destPath));
        String line;
        while ((line = br.readLine())!=null){
            bw.write(line);
            bw.newLine();
        }
        bw.close();
        br.close();
    }
}
